package autocode

import (
	"time"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
)

type UserGoodsInfoService struct {
}

// CreateUserGoodsInfo 创建UserGoodsInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (userGoodsInfoService *UserGoodsInfoService) CreateUserGoodsInfo(userGoodsInfo autocode.UserGoodsInfo) (err error) {
	err = global.GVA_DB.Create(&userGoodsInfo).Error
	return err
}

// DeleteUserGoodsInfo 删除UserGoodsInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (userGoodsInfoService *UserGoodsInfoService) DeleteUserGoodsInfo(userGoodsInfo autocode.UserGoodsInfo) (err error) {
	err = global.GVA_DB.Delete(&userGoodsInfo).Error
	return err
}

// DeleteUserGoodsInfoByIds 批量删除UserGoodsInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (userGoodsInfoService *UserGoodsInfoService) DeleteUserGoodsInfoByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]autocode.UserGoodsInfo{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateUserGoodsInfo 更新UserGoodsInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (userGoodsInfoService *UserGoodsInfoService) UpdateUserGoodsInfo(userGoodsInfo autocode.UserGoodsInfo) (err error) {
	err = global.GVA_DB.Save(&userGoodsInfo).Error
	return err
}

// GetUserGoodsInfo 根据id获取UserGoodsInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (userGoodsInfoService *UserGoodsInfoService) GetUserGoodsInfo(id uint) (err error, userGoodsInfo autocode.UserGoodsInfo) {
	err = global.GVA_DB.Where("id = ?", id).First(&userGoodsInfo).Error
	return
}

// GetUserGoodsInfoInfoList 分页获取UserGoodsInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (userGoodsInfoService *UserGoodsInfoService) GetUserGoodsInfoInfoList(info autoCodeReq.UserGoodsInfoSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&autocode.UserGoodsInfo{}).Order("user_goods_info.create_time desc")
	var userGoodsInfos []autocode.UserGoodsInfo
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.EndData != nil && info.StartData != nil {
		db.Where("user_goods_info.create_time >= ? AND user_goods_info.create_time <= ?", time.Unix(info.StartData.Unix(), 0), time.Unix(info.EndData.Unix()+86400-1, 0))
	}
	if info.SourceType != nil && *info.SourceType != 0 {
		db = db.Where("source_type = ?", info.SourceType)
	}
	if info.UserId != nil && *info.UserId != 0 {
		db = db.Where("user_id = ?", info.UserId)
	}
	if info.Status != nil {
		db = db.Where("status = ?", info.Status)
	}
	if info.Type != nil && *info.Type != 0 {
		db = db.Where("user_goods_info.type = ?", info.Type)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	//err = db.Limit(limit).Offset(offset).Find(&userGoodsInfos).Error
	selects := "user_goods_info.source_type, user_goods_info.goods_id, user_goods_info.create_time, user_goods_info.user_id, user_goods_info.created_at, user_goods_info.status, user_goods_info.goods_value, user_goods_info.type, user_info.user_name, user_info.open_id, goods_info.goods_name"
	err = db.Limit(limit).Offset(offset).Joins("left join user_info on user_info.id = user_goods_info.user_id").Joins("left join goods_info on goods_info.id = user_goods_info.goods_id").Select(selects).Find(&userGoodsInfos).Error
	return err, userGoodsInfos, total
}
